package com.hzw.saas.service.project.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.storage.IResourceFileService;
import com.hzw.saas.api.storage.bo.ResourceFileBO;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.service.project.bo.ProjectAttachmentBO;
import com.hzw.saas.service.project.mapper.ProjectAttachmentMapper;
import com.hzw.saas.service.project.model.ProjectAttachment;
import com.hzw.saas.service.project.service.IProjectAttachmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zzl
 * @since 08/11/2021
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProjectAttachmentServiceImpl extends ServiceImpl<ProjectAttachmentMapper, ProjectAttachment> implements IProjectAttachmentService {

    private final IResourceFileService resourceFileService;

    @Override
    public List<ProjectAttachmentBO> listAttachmentBo(String projectApplyId, Integer... attachmentType) {
        List<ProjectAttachment> attachments = this.listProjectAttachment(projectApplyId, attachmentType);
        List<ProjectAttachmentBO> attachmentBOS = MapperUtil.nf().mapAsList(attachments, ProjectAttachmentBO.class);
        List<String> resourceIds = attachmentBOS.stream().map(ProjectAttachmentBO::getResourceId).collect(Collectors.toList());

        List<ResourceFileBO> attachmentFiles = resourceFileService.listFiles(resourceIds);
        Map<String, ResourceFileBO> fileMap = attachmentFiles.stream().collect(Collectors.toMap(
            ResourceFileBO::getPid,
            bo -> bo,
            (t1, t2) -> t1
        ));

        for (ProjectAttachmentBO attachmentBO : attachmentBOS) {
            ResourceFileBO resourceFileBO = fileMap.get(attachmentBO.getResourceId());
            if (resourceFileBO == null) {
                continue;
            }
            attachmentBO.setResourceName(resourceFileBO.getName());
            attachmentBO.setResourceSize(resourceFileBO.getLength());
        }

        return attachmentBOS;
    }

    @Override
    public List<ProjectAttachment> listProjectAttachment(String projectApplyId, Integer... attachmentType) {
        return super.lambdaQuery()
            .eq(ProjectAttachment::getProjectApplyId, projectApplyId)
            .in(ArrayUtil.isNotEmpty(attachmentType), ProjectAttachment::getAttachmentType, attachmentType)
            .list();
    }

    @Override
    public List<String> listProjectAttachmentIds(String projectApplyId, Integer... attachmentType) {
        List<ProjectAttachment> list = super.lambdaQuery()
            .select(ProjectAttachment::getResourceId)
            .eq(ProjectAttachment::getProjectApplyId, projectApplyId)
            .in(ArrayUtil.isNotEmpty(attachmentType), ProjectAttachment::getAttachmentType, attachmentType)
            .list();
        return list.stream().map(ProjectAttachment::getResourceId).collect(Collectors.toList());
    }

    @Override
    public void deleteAttachments(List<String> resourceIds) {
        if (CollUtil.isEmpty(resourceIds)) {
            return;
        }
        super.lambdaUpdate()
            .in(ProjectAttachment::getResourceId, resourceIds)
            .remove();
    }
}
